package com.john.permission.web;

import com.alibaba.fastjson.JSON;
import com.john.permission.pojo.LayUITreePOJO;
import com.john.permission.pojo.Permission;
import com.john.permission.pojo.Role;
import com.john.permission.service.PermissionService;
import com.john.permission.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/to_modify")
    public String toRoleModifyPage(Model model) {

        List<Role> roleList = new ArrayList<>();
        Role role = new Role();
        role.setId(1);
        roleList.add(role);

        // 当前角色已经拥有的权限
        List<Permission> userPermission = permissionService.getUserPermission(roleList);

        // 当前系统中的全部权限
        List<Permission> allPermissions = permissionService.getAllPermissions();

        // 将所有权限按照父子关系转成一颗权限树
        List<Permission> permissionTree = permissionService.buildPermissionTree(allPermissions);

        List<LayUITreePOJO> layUITreePOJOList = permissionService.convertPermissionTree2LayUITree(permissionTree, userPermission);

        String layUITreeJson = JSON.toJSONString(layUITreePOJOList);

        model.addAttribute("permissionTree", layUITreeJson);

        return "modify_role";
    }

    @RequestMapping("/to_rolelist")
    public String toRoleListPage() {
        return "rolelist";
    }

}
